<!--
 * @Description: zzb
 * @Version: 0.0.1
 * @Autor: zzb
 * @Date: 2022-12-06 16:31:42
 * @LastEditors: zzb
 * @LastEditTime: 2022-12-20 16:19:10
-->
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
import resize from "../mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "500px",
    },
    options: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    options: {
      handler(val) {
        console.log("val: options", val);
        // if (!this.chart) {
        //   this.initChart();
        // }
        this.chart && this.setChart();
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      console.log("this.$el: 11", this.$el);
      this.chart = echarts.init(this.$el);
      this.setChart();
    },
    setChart() {
      this.chart.setOption(this.options);
      // console.log(this.$el.className);
      if (this.className == "depDetail") {
        this.chart.on("click", (params) => {
          this.$emit("handleDepDetail", params.name);
        });
      } else if (this.className == "companyDetail") {
        this.chart.on("click", (params) => {
          this.$emit("handleDepDetail", params.name);
        });
      }
    },
    destroyChart() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
  },
};
</script>
